package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import Classes.Disciplina;
import Repositorio.Rep_Disciplina;

import java.awt.Window.Type;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

public class CadDisciplina extends JFrame {

	private JPanel contentPane;
	private JTextField txtNomeDisc;
	private JTextArea txtDescEmenta;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CadDisciplina frame = new CadDisciplina();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			
		}
	}

	/**
	 * Create the frame.
	 */
	public CadDisciplina() 
	{
		JOptionPane.setRootFrame(this);
		setResizable(false);
		setAlwaysOnTop(true);
		setResizable(false);
		setType(Type.NORMAL);
		
		setTitle("Cadastro de Disciplina");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 628, 422);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 11, 582, 59);
		panel.add(panel_1);
		
		JLabel lblCadastroDeDisciplina = new JLabel("Cadastro de Disciplina");
		lblCadastroDeDisciplina.setFont(new Font("Tahoma", Font.PLAIN, 22));
		lblCadastroDeDisciplina.setBounds(169, 12, 222, 33);
		panel_1.add(lblCadastroDeDisciplina);
		
		JPanel panel_2 = new JPanel();
		panel_2.setLayout(null);
		panel_2.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(10, 81, 582, 248);
		panel.add(panel_2);
		
		txtNomeDisc = new JTextField();
		txtNomeDisc.setColumns(10);
		txtNomeDisc.setBounds(115, 11, 450, 20);
		panel_2.add(txtNomeDisc);
		
		txtDescEmenta = new JTextArea();
		txtDescEmenta.setColumns(10);
		txtDescEmenta.setBounds(115, 70, 450, 111);
		panel_2.add(txtDescEmenta);
		
		JLabel label_1 = new JLabel("Nome:");
		label_1.setBounds(10, 14, 46, 14);
		panel_2.add(label_1);
		
		JLabel lblCargaHoraria_1 = new JLabel("Carga Horaria:");
		lblCargaHoraria_1.setBounds(10, 45, 95, 14);
		panel_2.add(lblCargaHoraria_1);
		
		JLabel lblCargaHoraria = new JLabel("Descricao Ementa:");
		lblCargaHoraria.setBounds(10, 73, 95, 14);
		panel_2.add(lblCargaHoraria);
		
		final JSpinner spCargaHor = new JSpinner();
		spCargaHor.setModel(new SpinnerNumberModel(1, 1, 1000, 1));
		spCargaHor.setBounds(115, 39, 70, 20);
		panel_2.add(spCargaHor);
		
		JLabel lblProfessor = new JLabel("Professor:");
		lblProfessor.setBounds(10, 206, 95, 14);
		panel_2.add(lblProfessor);
		
		Rep_Disciplina rep_disc = new Rep_Disciplina();
		final JComboBox cmbProf = new JComboBox();
		DefaultComboBoxModel cmbmodel = new DefaultComboBoxModel(rep_disc.ListarProfessor().toArray());
		cmbProf.setModel(cmbmodel);
		
		
		cmbProf.setBounds(115, 203, 450, 20);
		panel_2.add(cmbProf);
		
		
		
		JButton button = new JButton("Salvar");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				int teste = 0;
				
				Disciplina disc = new Disciplina();
				Rep_Disciplina rep_disc = new Rep_Disciplina();
				
				disc.setNome(txtNomeDisc.getText());
				disc.setCarga_horaria((int) spCargaHor.getValue());
				disc.setDescricao_ementa(txtDescEmenta.getText());
				disc.setId_professor(rep_disc.PegaIDProf(cmbProf.getSelectedItem().toString()));
				
				rep_disc.InserirDisciplinaDB(disc);			
				
				
			}
		});
		button.setBounds(14, 340, 136, 32);
		panel.add(button);
		
		JButton button_1 = new JButton("Cancelar");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				dispose();
			}
		});
		button_1.setBounds(450, 340, 142, 32);
		panel.add(button_1);
		
	}
}
